package com.lepu.app.fun.chat.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.app.utils.Setting;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.widget.CircularImageView;
import com.lepu.app.fun.chat.bean.BeanLectureChatUser;
import com.lepu.pasm.R;

import java.util.ArrayList;

public class MembersSortAdapter extends BaseAdapter implements SectionIndexer {

    private LayoutInflater mLayoutInflater = null;
    @SuppressWarnings("unused")
    private Context mContext = null;
    private ArrayList<BeanLectureChatUser> mArrayList = null;

    public MembersSortAdapter(Context context, ArrayList<BeanLectureChatUser> arrayList) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mArrayList = arrayList;
    }

    @Override
    public int getCount() {
        int size = 0;
        if (mArrayList != null && mArrayList.size() > 0) {
            size = mArrayList.size();
        }
        return size;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        final BeanLectureChatUser item = mArrayList.get(position);

        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.chat_group_members_list_item, null);

            holder = new ViewHolder();

            CircularImageView iconImageView = (CircularImageView) convertView
                    .findViewById(R.id.iconImageView);
            TextView titleTextView = (TextView) convertView
                    .findViewById(R.id.titleTextView);
            TextView catalogTextView = (TextView) convertView
                    .findViewById(R.id.catalogTextView);

            holder.iconImageView = iconImageView;
            holder.titleTextView = titleTextView;
            holder.catalogTextView = catalogTextView;

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);


        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if(position == getPositionForSection(section)){
            holder.catalogTextView.setVisibility(View.VISIBLE);
            holder.catalogTextView.setText(item.NickNameInitials.toUpperCase());

        }else{
            holder.catalogTextView.setVisibility(View.GONE);
        }

        holder.titleTextView.setText(item.NickName);
        UniversalImageLoadTool.disPlay(Setting.getHeadIconUrl(item.UserID), holder.iconImageView,
                R.drawable.app_ic_default_avatar);

        return convertView;
    }

//    public void updateListView(ArrayList<BeanLectureChatUser> filterDateList) {
//
//        this.mArrayList = filterDateList;
//        notifyDataSetChanged();
//    }

    private static class ViewHolder {

        CircularImageView iconImageView;
        TextView titleTextView, catalogTextView;

    }

    @Override
    public Object[] getSections() {
//        return new Object[0];
        return null;
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */

    @Override
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = mArrayList.get(i).NickNameInitials;
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */

    @Override
    public int getSectionForPosition(int position) {
        return mArrayList.get(position).NickNameInitials.charAt(0);
    }


}
